package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import com.mars.module.admin.entity.NewsScore;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.News;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.INewsService;
import com.mars.module.admin.request.NewsRequest;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "1.新闻接口管理", tags = "1.新闻接口管理")
@RequestMapping("/admin/news")
public class NewsController {

    private final INewsService iNewsService;

    /**
     * 分页查询新闻列表
     */
    @ApiOperation(value = "分页查询新闻列表")
    @PostMapping("/pageList")
    public R<PageInfo<News>> pageList(@RequestBody NewsRequest newsRequest) {
        return R.success(iNewsService.pageList(newsRequest));
    }

    @ApiOperation(value = "分页查询系统推荐的新闻列表")
    @PostMapping("/pageRecommendList")
    public R<PageInfo<News>> pageRecommendList() {
        return R.success(iNewsService.pageRecommendList());
    }

    /**
     * 获取新闻详细信息
     */
    @ApiOperation(value = "获取新闻详细信息")
    @GetMapping(value = "/query/{id}")
    public R<News> detail(@PathVariable("id") Long id) {
        return R.success(iNewsService.getById(id));
    }

    /**
     * 新增新闻
     */
    @Log(title = "新增新闻", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增新闻")
    @PostMapping("/add")
    public R<Void> add(@RequestBody NewsRequest newsRequest) {
        iNewsService.add(newsRequest);
        return R.success();
    }

    /**
     * 修改新闻
     */
    @Log(title = "修改新闻", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改新闻")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody NewsRequest newsRequest) {
        iNewsService.update(newsRequest);
        return R.success();
    }

    /**
     * 删除新闻
     */
    @Log(title = "删除新闻", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除新闻")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iNewsService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
     * 导出新闻
     *
     * @param response response
     * @throws IOException IOException
     */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response, @RequestBody NewsRequest newsRequest) throws IOException {
        List<News> list = iNewsService.list(newsRequest);
        ExcelUtils.exportExcel(list, News.class, "新闻信息", response);
    }
}
